﻿using System;
using System.Collections;
using System.Collections.Generic;

using IBatisNet.DataAccess;

using AUV5.Model;
using AUV5.DAL;
using AUV5.DAL.Interface;
using AUV5.DAL.Dao;

namespace AUV5.BLL
{
	/// <summary>
    /// 功能：     FailedLogins业务逻辑实现
    /// 作者：     LI
    /// 日期：     2011/12/8
    /// 修改日期： 
    /// 修改人：
    /// 修改内容：
    /// </summary>
	public class FailedLoginsBLL
	{
		private static FailedLoginsBLL instance = null;
		private static object syncRoot = new object();
        private IDaoManager daoManager = ServiceConfig.Instance.DaoManager;
        private IFailedLoginsDao dao = null;
		
		private FailedLoginsBLL()
        {
            dao = FailedLoginsSqlMapDao.Instance;
        }
		
		 public static FailedLoginsBLL Instance
        {
            get 
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new FailedLoginsBLL();
                        }
                    }
                }
                return instance;
            }
        }
		#region	基本功能 不能修改
		
		
		/// <summary>
		/// 得到最大行数
		/// </summary>
		/// <returns></returns>
        public int GetMaxCounts()
        {
            try
            {
                //daoManager.BeginTransaction();

                int count = dao.GetMaxCounts();

                //daoManager.CommitTransaction();

                return count;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }
		
		/// <summary>
		/// 得到列表
		/// </summary>
		/// <returns></returns>
        public System.Collections.Generic.IList<FailedLogins> GetList()
        {
            try
            {
                //daoManager.BeginTransaction();

                System.Collections.Generic.IList<FailedLogins> list = dao.GetList();

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }
		
		/// <summary>
		/// 通过不同参数得到列表
		/// </summary>
		/// <param name="TopCounts"></param>
		/// <returns></returns>
        private System.Collections.Generic.IList<FailedLogins> GetListByParameters(object TopCounts, FailedLogins failedlogins)
        {
            try
            {
                //daoManager.BeginTransaction();
				
				Hashtable ht = new Hashtable();
				ht.Add("TopCounts",TopCounts);
                System.Collections.Generic.IList<FailedLogins> list = dao.GetListByParameters(ht);

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }
		
		
		/// <summary>
		/// 得到明细
		/// </summary>
		/// <param name="IpAddress"></param>
		/// <returns></returns>
		public FailedLogins GetDetail(System.String ipaddress)
		{
			try
            {
                ///daoManager.BeginTransaction();

                return dao.GetDetail(ipaddress);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
		}
		
		
		/// <summary>
        /// 新建
        /// </summary>
        /// <param name="failedlogins"></param>
        /// <returns></returns>
		public object Add(FailedLogins failedlogins)
		{
			//int id = GetId("SYS_UserInfoTemp");
			//sys_userinfotemp.UserID = id;
			
			try
            {
                ///daoManager.BeginTransaction();

                return dao.Add(failedlogins);
                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
		}
		
		/// <summary>
        /// 修改
        /// </summary>
        /// <param name="failedlogins"></param>
        /// <returns></returns>
		public int Update(FailedLogins failedlogins)
		{
			try
            {
                ///daoManager.BeginTransaction();

                return dao.Update(failedlogins);
		
				///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
		}

		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="IpAddress"></param>
		/// <returns></returns>
		public int Delete(System.String ipaddress)
		{
			try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(ipaddress);
	
				///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
		}
		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="failedlogins"></param>
		/// <returns></returns>
		public int Delete(FailedLogins failedlogins)
		{
			try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(failedlogins);
	
				///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
		}
		
		#endregion
		
		#region 扩展功能

        /// <summary>
        /// 增加指定IP的错误记录数
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public int AddFailedLoginCount(string ip)
        {
            try
            {
                FailedLogins log = GetDetail(ip);
                if (log != null)
                {
                    log.ErrorCount += 1;
                    return Update(log);
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        /// <summary>
        /// 将指定IP的错误记录数置为1 ，没有记录则添加
        /// </summary>
        /// <param name="ip"></param>
        public void AddOrResetFailedLoginRecord(string ip)
        {
            try
            {
                FailedLogins log = GetDetail(ip);
                if (log != null)
                {
                    log.ErrorCount = 1;
                    log.LastUpdate = DateTime.Now;
                    Update(log);
                }
                else
                {
                    log = new FailedLogins();
                    log.IpAddress = ip;
                    log.ErrorCount = 1;
                    log.LastUpdate = DateTime.Now;
                    Add(log);
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        

		#endregion
	}
}
